package com.mappn.backup.util;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.preference.PreferenceManager;

public class PrefUtil {
    public static SharedPreferences sPref = null;
    // 最后备份时间
    private static final String P_LAST_BACKUP = "pref.last.backup";
    // 备份提醒时间
    private static final String P_NOTI_BACKUP = "pref.noti.backup";
    // 是否第一次登录
    public static final String P_IS_FIRST = "pref.is.first";

    public synchronized static void init(Context context) {
        if (sPref == null) {
            sPref = PreferenceManager.getDefaultSharedPreferences(context);
        }
    }

    public static long getLastBackup(Context context) {
        if (sPref == null) {
            init(context);
        }
        return sPref.getLong(P_LAST_BACKUP, 0);
    }

    public static void setLastBackup(Context context, long lastBackup) {
        if (sPref == null) {
            init(context);
        }
        Editor edit = sPref.edit();
        edit.putLong(P_LAST_BACKUP, lastBackup);
        edit.commit();
    }

    public static long getNotiBackup(Context context) {
        if (sPref == null) {
            init(context);
        }
        return sPref.getLong(P_NOTI_BACKUP, Const.DEFAULT_NOTI_TIME);
    }

    public static void setNotiBackup(Context context, long notiBackup) {
        if (sPref == null) {
            init(context);
        }
        Editor edit = sPref.edit();
        edit.putLong(P_NOTI_BACKUP, notiBackup);
        edit.commit();
    }

    public static boolean isFirst(Context context) {
        if (sPref == null) {
            init(context);
        }
        return sPref.getBoolean(P_IS_FIRST, true);
    }

    public static void setIsFirst(Context context, boolean isFirst) {
        if (sPref == null) {
            init(context);
        }
        Editor edit = sPref.edit();
        edit.putBoolean(P_IS_FIRST, isFirst);
        edit.commit();
    }
}
